package com.jiantong.standalone;

import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.work.model.BeastsBreedStockMessage;
import com.jiantong.work.service.BeastsBreedStockMessageWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时器,定时每天把养殖场信息存入合计记录信息
 * Created by mz03 on 2017/4/18.
 */
@Slf4j
@Component
public class BreedTimer {
//    private final BreedFactoryDao breedFactoryDao;
//    private final BeastsBreedStockMessageDao beastsBreedStockMessageDao;
//
//    @Autowired
//    public BreedTimer(BreedFactoryDao breedFactoryDao,BeastsBreedStockMessageDao beastsBreedStockMessageDao){
//        this.breedFactoryDao = breedFactoryDao;
//        this.beastsBreedStockMessageDao = beastsBreedStockMessageDao;
//    }
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;

    @RpcConsumer
    private BeastsBreedStockMessageWriteService beastsBreedStockMessageWriteService;

    /**
     * 定时器, 每天0点 从养殖场表djt_breed_factories 查出存栏合计存入 djt_beasts_breed_stock_message
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void grabQuoteOnline(){
        try{
            System.out.println(
                    "this \nis \nbreed \ntime \n>>>>>>>>>>>>>>\n>>>>>>>>>>"
            );
            //查询所有养殖场  status:1 的状态是生效状态 0 为删除状态
            List<BreedFactory> list = breedFactoryReadService.findByStatus1(1).getResult();
            List<BeastsBreedStockMessage> beastsBreedStockMessageList = new ArrayList<>();
            for (int i = 0; i<list.size(); i++){

                BeastsBreedStockMessage beastsBreedStockMessage = new BeastsBreedStockMessage();
                beastsBreedStockMessage.setStatus(1);
                beastsBreedStockMessage.setBreedFactoryId(list.get(i).getId());
                beastsBreedStockMessage.setBreedFactoryName(list.get(i).getName());
                beastsBreedStockMessage.setBreedType(list.get(i).getBreedType());
                beastsBreedStockMessage.setStableNumber(list.get(i).getStableNumber());
                beastsBreedStockMessage.setOwnerId(list.get(i).getUserId());
                beastsBreedStockMessage.setStockQuantity(list.get(i).getStockQuantity());
                beastsBreedStockMessage.setChangeDate(new Date());
                beastsBreedStockMessageList.add(beastsBreedStockMessage);

     //           beastsBreedStockMessageDao.create(beastsBreedStockMessage);
                log.info("this \nis \nbreed \ntime \n>>>>>>>>>>>>>>\n>>>>>>>>>>");
            }
            beastsBreedStockMessageWriteService.createBeastsBreedStockMessages(beastsBreedStockMessageList);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
